var docEl = document.documentElement,
    //当设备的方向变化（设备横向持或纵向持）此事件被触发。绑定此事件时，
    //注意现在当浏览器不支持orientationChange事件的时候我们绑定了resize 事件。
    //总来的来就是监听当然窗口的变化，一旦有变化就需要重新设置根字体的值
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function() {
        //设置根字体大小
        var maxWidth = 750
        var cw = docEl.clientWidth > maxWidth ? maxWidth : docEl.clientWidth;
        docEl.style.fontSize = 20 * (cw / 375) + 'px';
    };

//绑定浏览器缩放与加载时间
window.addEventListener(resizeEvt, recalc, false);
document.addEventListener('DOMContentLoaded', recalc, false);
//正式服务器
var url = "http://store.amyun.cn/StoreClient/";

//测试服务器
// var url = "http://tps.amyun.cn/StoreClient/";

//本地服务器
// var url = "http://172.16.20.176:8080/StoreClient/"

/************获取bossId*********************/
function getBossId() {
    return bossId = window.localStorage.getItem('id');
}
/*********获取Id******************/
function setBossId(id) {
    window.localStorage.setItem("id", id); //保存数据
}
/*********loading******************/

/**
 * 取得客户端类型
 */
function getClientType() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('micromessenger') > -1) {
        return "wechat";
    } else if (ua.indexOf('amezios') > -1) {
        return "ios";
    } else if (ua.indexOf('amezandroid') > -1) {
        return "android";
    } else {
        return "wap";
    }
}

/**
 * 判断是否为APP
 */
function isApp() {
    return (getClientType() == "ios" || getClientType() == "android");
}

/**
 * 判断是否为iOS
 */
function isIOS() {
    return getClientType() == "ios";
}

/**
 * 判断是否为Android
 */
function isAndroid() {
    return getClientType() == "android";
}

/**
 * 设备桥接方法
 */
if (isApp()) {

    if (isIOS()) {
        function setupWebViewJavascriptBridge(callback) {

            if (window.WebViewJavascriptBridge) {
                return callback(WebViewJavascriptBridge);
            }
            if (window.WVJBCallbacks) {
                return window.WVJBCallbacks.push(callback);
            }
            window.WVJBCallbacks = [callback];
            var WVJBIframe = document.createElement('iframe');
            WVJBIframe.style.display = 'none';
            WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function() {
                document.documentElement.removeChild(WVJBIframe)
            }, 0)
        }

        setupWebViewJavascriptBridge(function(bridge) {
            // bridge.registerHandler('setUserInfo', function (data, responseCallback) {
            //     alert(11);
            //     var id=data.bossId;
            //     setBossId(id);
            // })
        })
    }



    // APP getInfo  bossId -- 老板ID lastStore 门店ID mobile 手机号 token 
    function getAppUserInfo(fn) {
        WebViewJavascriptBridge.callHandler('getAppUserInfo', {}, function(response) {
            fn(response)
        })
    }

    // APP 返回
    function AppGoBack() {
        WebViewJavascriptBridge.callHandler('appGoBack', {
            'param': 'aaaaa'
        }, function(response) {

        })
    }

    // APP 清除某一Cookie
    function ClearCookie(param) {
        WebViewJavascriptBridge.callHandler('clearCookie', param, function(response) {

        })
    }

    // APP 清除所有Cookie
    function ClearAllCookie() {
        WebViewJavascriptBridge.callHandler('clearAllCookie', param, function(response) {

        })
    }

    // APP Log日志调试
    function Log(param) {

        WebViewJavascriptBridge.callHandler('LogClick', param, function(response) {

        })
    }

    // APP微信公众号
    function JumpWXPublic(param, fn) {
        WebViewJavascriptBridge.callHandler('JumpWXPublicClick', param, function(response) {
            fn(response)
        })
    }

    // 扫一扫功能
    function QRScan(fn) {
        WebViewJavascriptBridge.callHandler('QRScanClick', {}, function(response) {
            fn(response)
        })
    }

    // 获取经纬度 
    function getlonlat(fn) {
        WebViewJavascriptBridge.callHandler('getlonlatClick', {}, function(response) {
            fn(response)
        })
    }

    // 关闭HUD
    function closeHUD() {

        WebViewJavascriptBridge.callHandler('closeHUDClick', {}, function(response) {
            console.log(response);
        })
    }

    // 复制
    function copySn(sn) {
        var param = {
            "sn": sn
        };
        WebViewJavascriptBridge.callHandler('copyClick', param, function(response) {
            console.log(response);
        })

    }

    // 清楚缓存
    function clearCache() {
        var param = {
            "title": "cache"
        };
        WebViewJavascriptBridge.callHandler('clearCacheClick', param, function(response) {
            console.log(response);
        })
    }

    // 支付宝支付
    function aliPay(param) {

        WebViewJavascriptBridge.callHandler('aliPayClick', param, function(response) {

            console.log(response);
        })
    }

    // 微信支付
    function wxPay(param) {

        WebViewJavascriptBridge.callHandler('wxPayClick', param, function(response) {

            console.log(response);
        })
    }

    // 微信好友列表分享
    function shareWXSession() {
        var param = {
            "title": "wxfx"
        };
        WebViewJavascriptBridge.callHandler('shareWXSessionClick', param, function(response) {
            //调用原生的分享成功后， oc返回分享结果给JS
            console.log(response);
        })
    }
    // 微信朋友圈分享
    function shareWXTimeLine() {
        var param = {
            "title": "wxfx"
        };
        WebViewJavascriptBridge.callHandler('shareWXTimeLineClick', param, function(response) {
            //调用原生的分享成功后， oc返回分享结果给JS
            console.log(response);
        })
    }
    // 新浪微博分享
    function shareSina() {
        var param = {
            "title": "qqfx"
        };
        WebViewJavascriptBridge.callHandler('shareSinaClick', param, function(response) {
            //调用原生的分享成功后， oc返回分享结果给JS
            console.log(response);
        })
    }
    // qq空间分享
    function shareQQZone() {

        var param = {
            "title": "qqfx"
        };
        WebViewJavascriptBridge.callHandler('shareQQZoneClick', param, function(response) {
            //调用原生的分享成功后， oc返回分享结果给JS
            console.log(response);
        })

    }
    // qq好友分享
    function shareQQ() {

        var param = {
            "title": "qqfx"
        };
        WebViewJavascriptBridge.callHandler('shareQQClick', param, function(response) {
            //调用原生的分享成功后， oc返回分享结果给JS
            console.log(response);
        })

    }
    /** APP微信授权登录
     *  授权信息  uid  openid  accessToken  refreshToken
     *  用户信息 name iconurl gender
     */
    function wxLogin(fn) {
        var param = {};
        WebViewJavascriptBridge.callHandler('wxLoginClick', param, function(response) {
            fn(response)
        })
    }




    // qq登录
    function qqLogin(fn) {
        var param = {};
        WebViewJavascriptBridge.callHandler('qqLoginClick', param, function(response) {
            fn(response)
        })
    }

    // 请求用户登录数据
    function getUserInfo() {
        WebViewJavascriptBridge.callHandler(
            'getUserInfo', {
                'param': 'getUserInfo'
            },
            function(responseData) {

            }
        );
    }

    // 打开照相机
    function openCarama(parma, fn) {
        WebViewJavascriptBridge.callHandler('openCaramaClick', param, function(response) {
            fn(response)
        })
    }
    //返回判断
    function back() {
        if (isIOS()) {
            history.go(-1);
        } else if (isAndroid()) {
            AppGoBack()
        }
    }

    //loading加载
    apiready = function() {
        api.parseTapmode();
    }
    var toast = new auiToast({});

    function showDefault(type) {
        switch (type) {
            case "loading":
                toast.loading({
                    title: "加载中",
                    duration: 2000
                }, function(ret) {

                });
                break;
            case "fail":
                toast.fail({
                    title: "失败",
                    duration: 2000
                });
                break;
        }
    }

}